package com.aijk.web.admin.controller.openapi;


import com.aijk.ehm.api.SemRecordApi;
import com.aijk.ehm.model.HealthMonitorQo;
import com.aijk.web.admin.controller.BaseController;
import com.alibaba.fastjson.JSONObject;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

/**
 * 健康监测平台接口
 *
 * @author Administrator
 */
@Controller
@RequestMapping(value = "/hm")
public class HealthMonitoringController extends BaseController {
    private static final Logger LOGGER = LoggerFactory.getLogger(HealthMonitoringController.class);

    @Autowired
    private SemRecordApi semRecordApi;

    /**
     * 接收监测平台的通知记录
     *
     * @param body 自检记录
     */
    @RequestMapping(value = "notify")
    @ResponseBody
    public HmResult notify(@RequestBody String body) {
        LOGGER.info("健康监测平台数据推送....");
        LOGGER.info("健康监测平台数据推送, 入参 req = " + body);

        HmResult result = new HmResult();
        try {
            HealthMonitorQo qo = new HealthMonitorQo();
            qo = JSONObject.parseObject(body, HealthMonitorQo.class);
            if (qo == null) {
                throw new Exception();
            }
            semRecordApi.saveHm(qo);

            LOGGER.info("转换, 入参  = " + qo);
//            pcnHmRecordService.saveRecord(req);
            result.setCode(0);
            result.setMsg("数据接收成功");
        } catch (Exception e) {
            result.setCode(500);
            result.setMsg("接收健康监测数据失败");
            LOGGER.error("接收健康监测数据失败", e);
        }

        return result;
    }



    class HmResult {
        private Integer code;
        private String msg;

        public Integer getCode() {
            return code;
        }

        public void setCode(Integer code) {
            this.code = code;
        }

        public String getMsg() {
            return msg;
        }

        public void setMsg(String msg) {
            this.msg = msg;
        }
    }
}
